/*
 * 
 */
package server.http;

import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Sérgio C. Peres
 * @author Guilherme Alves
 */
public class HTTPResponse
{

    private HTTPServer server;
    private DataOutputStream output;
    private HTTPRequest request;
    private String addressReq;
    private boolean isPOSTRequest;

    public HTTPResponse(HTTPServer srv, DataOutputStream output)
    {
        this.output = output;
        this.server = srv;
        this.isPOSTRequest = false;
    }

    public boolean setRequest(HTTPRequest request)
    {
        try
        {
            this.request = request;
            String s = request.getHeader();
            int i;
            String r = s.substring(0, 3);
            if(!r.equals("GET"))
            {
                this.isPOSTRequest = true;
            }
            else
            {
                for (i = 5; (i < s.length()) && (s.charAt(i) != ' '); i++);
                addressReq = s.substring(5, i);
            }
        } catch (StringIndexOutOfBoundsException e)
        {
            return false;
        }
        return true;
    }

    public void sendResponse() throws IOException
    {
        if (!this.isPOSTRequest){
        this.readFile();
        }
        else
        {
            output.writeBytes(returnHTMLError(400, null));
        }
        output.close();
    }

    public void readFile() throws IOException
    {
        FileReader in;
        try
        {
            in = new FileReader(this.server.getPathDir() + "\\" + this.addressReq);
            try
            {

                int i;
                for (i = in.read(); i != -1; i = in.read())
                {
                    output.writeByte(i);
                }

                in.close();
            } catch (IOException ex)
            {
                output.writeBytes(returnHTMLError(404, this.addressReq));
            }
        } catch (FileNotFoundException ex)
        {
            output.writeBytes(returnHTMLError(404, this.addressReq));
        }
    }

    private String returnHTMLError(int errorCode, String req)
    {
        String res = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
                + "<html><head>\n";
        switch (errorCode)
        {
            case 404:
                res = "<title>404 Not Found</title>\n"
                        + "</head><body>\n"
                        + "<h1>Not Found</h1>\n"
                        + "<p>The requested URL " + req + " was not found on this server.</p>\n"
                        + "<p>Additionally, a 404 Not Found\n"
                        + "error was encountered while trying to use an ErrorDocument to handle the request.</p>\n"
                        + "<hr>\n";

                break;
                
            case 400:
                res = "<title>400 Bad Request</title>\n"
                        + "</head><body>\n"
                        + "<h1>Bad Request</h1>\n"
                        + "<hr>\n";

                break;
        }

        res += "<address>Web Server at " + "" + " Port " + this.server.getPort() + "</address>\n"
                + "</body></html>";
        return res;
    }
}